package library.books_author;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import library.LongRunningProcess;
import library.utils.Utils;
import library.database.Data;
import library.model.BooksAuthorPojo;

/**
 * @author Dmitry
 */
public class BookAuthorPanel extends JPanel {
    private JTable table;
    private BooksAuthorTableModel tableModel;
    private OnBookAuthorSelected event;

    public interface OnBookAuthorSelected {
        public void onSelect(BooksAuthorPojo object);
    }

    public BookAuthorPanel() {
        init();
    }

    private void init() {
        setLayout(new BorderLayout());
        tableModel = createTableModel();
        table = new JTable(tableModel);
        table.setFont(Utils.getDefaultFont());
        table.setRowSelectionAllowed(true);
        JScrollPane scroll = new JScrollPane(table);
        add(scroll);
        for (int i = 0; i < table.getColumnCount(); i++) {
            table.getColumnModel().getColumn(i).setCellRenderer(new BooksAuthorCellRenderer());
        }

        table.setRowSelectionAllowed(false);
        table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (!e.getValueIsAdjusting()) {
                    if (event != null) {
                        int index = table.getSelectedRow();
                        if (index != -1) {
                            BooksAuthorPojo object = tableModel.getObject(index);
                            event.onSelect(object);
                        }
                    }
                }
            }
        });
    }

    private BooksAuthorTableModel createTableModel() {
        return new BooksAuthorTableModel();
    }

    public void setOnBookAuthorSelectedEvent(OnBookAuthorSelected event) {
        this.event = event;
    }

    public void setGenreFilter(final String genre) {
        LongRunningProcess loader = new LongRunningProcess((JFrame) SwingUtilities.getRoot(this)) {
            private BooksAuthorPojo[] data;

            @Override
            protected Object doInBackground() throws Exception {
                try {
                    data = Data.getBooksAuthorsByGenre(genre);
                } catch (Exception ex) {
                    Utils.errorDialog("Ошибка доступа к базе данных");
                    Utils.log(ex);
                }
                return null;
            }

            @Override
            protected void onDone() {
                tableModel.setData(data);
            }
        };
        loader.execute();
    }

    private class BooksAuthorCellRenderer extends DefaultTableCellRenderer {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            Component component = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            BooksAuthorPojo obj = tableModel.getObject(row);
            if (obj.hasDescription()) {
                component.setForeground(Color.BLUE);
            } else {
                component.setForeground(Color.BLACK);
            }
            if(hasFocus){
                component.setBackground(Color.GREEN);
            }else{
                component.setBackground(Color.WHITE);
            }
            return component;
        }
    }
}
